-- Orewood Reborn ABM Registry. I would like to thank desvox for making the orewood mod this one originated from.

-- Functions to sprout papy from logs

minetest.register_abm({
	nodenames = {"orewood:ironwood", "orewood:ironwood_jungle"},
	neighbors = {"default:water_source"},
	interval = 40,
	chance = 10,
	action = function(pos, node)
	local height = 0
	while node.name == "orewood:ironwood" and height < 4 do
		height = height+1
		pos.y = pos.y+1
		node = minetest.get_node(pos)
	end
	while node.name == "orewood:ironwood_jungle" and height < 4 do
		height = height+1
		pos.y = pos.y+1
		node = minetest.get_node(pos)
	end
	if height == 4
	or node.name ~= "air" then
		return
	end
	minetest.set_node(pos, {name="orewood:ironpapy"})
	end,
})

minetest.register_abm({
	nodenames = {"orewood:steelwood_hot"},
	neighbors = {"default:lava_source"},
	interval = 40,
	chance = 10,
	action = function(pos, node)
	local height = 0
	while node.name == "orewood:steelwood_hot" and height < 4 do
		height = height+1
		pos.y = pos.y+1
		node = minetest.get_node(pos)
	end
	if height == 4
	or node.name ~= "air" then
		return
	end
	minetest.set_node(pos, {name="orewood:steelpapy_ingot"})
	end,
})

minetest.register_abm({
	nodenames = {"orewood:coalwood", "orewood:coalwood_jungle"},
	neighbors = {"default:water_source"},
	interval = 40,
	chance = 10,
	action = function(pos, node)
	local height = 0
	while node.name == "orewood:coalwood" and height < 4 do
		height = height+1
		pos.y = pos.y+1
		node = minetest.get_node(pos)
	end
	while node.name == "orewood:coalwood_jungle" and height < 4 do
		height = height+1
		pos.y = pos.y+1
		node = minetest.get_node(pos)
	end
	if height == 4
	or node.name ~= "air" then
		return
	end
	minetest.set_node(pos, {name="orewood:coalpapy"})
	end,
})

minetest.register_abm({
	nodenames = {"orewood:tinwood", "orewood:tinwood_jungle"},
	neighbors = {"default:water_source"},
	interval = 40,
	chance = 10,
	action = function(pos, node)
	local height = 0
	while node.name == "orewood:tinwood" and height < 4 do
		height = height+1
		pos.y = pos.y+1
		node = minetest.get_node(pos)
	end
	while node.name == "orewood:tinwood_jungle" and height < 4 do
		height = height+1
		pos.y = pos.y+1
		node = minetest.get_node(pos)
	end	
	if height == 4
	or node.name ~= "air" then
		return
	end
	minetest.set_node(pos, {name="orewood:tinpapy"})
	end,
})

minetest.register_abm({
	nodenames = {"orewood:tinwood_hot"},
	neighbors = {"default:lava_source"},
	interval = 40,
	chance = 10,
	action = function(pos, node)
	local height = 0
	while node.name == "orewood:tinwood_hot" and height < 4 do
		height = height+1
		pos.y = pos.y+1
		node = minetest.get_node(pos)
	end
	if height == 4
	or node.name ~= "air" then
		return
	end
	minetest.set_node(pos, {name="orewood:tinpapy_ingot"})
	end,
})

minetest.register_abm({
	nodenames = {"orewood:copperwood", "orewood:copperwood_jungle"},
	neighbors = {"default:water_source"},
	interval = 40,
	chance = 10,
	action = function(pos, node)
	local height = 0
	while node.name == "orewood:copperwood" and height < 4 do
		height = height+1
		pos.y = pos.y+1
		node = minetest.get_node(pos)
	end
	while node.name == "orewood:copperwood_jungle" and height < 4 do
		height = height+1
		pos.y = pos.y+1
		node = minetest.get_node(pos)
	end
	if height == 4
	or node.name ~= "air" then
		return
	end
	minetest.set_node(pos, {name="orewood:copperpapy"})
	end,
})

minetest.register_abm({
	nodenames = {"orewood:copperwood_hot"},
	neighbors = {"default:lava_source"},
	interval = 40,
	chance = 10,
	action = function(pos, node)
	local height = 0
	while node.name == "orewood:copperwood_hot" and height < 4 do
		height = height+1
		pos.y = pos.y+1
		node = minetest.get_node(pos)
	end
	if height == 4
	or node.name ~= "air" then
		return
	end
	minetest.set_node(pos, {name="orewood:copperpapy_ingot"})
	end,
})

minetest.register_abm({
	nodenames = {"orewood:mesewood", "orewood:mesewood_jungle"},
	neighbors = {"default:water_source"},
	interval = 40,
	chance = 10,
	action = function(pos, node)
	local height = 0
	while node.name == "orewood:mesewood" and height < 4 do
		height = height+1
		pos.y = pos.y+1
		node = minetest.get_node(pos)
	end
	while node.name == "orewood:mesewood_jungle" and height < 4 do
		height = height+1
		pos.y = pos.y+1
		node = minetest.get_node(pos)
	end
	if height == 4
	or node.name ~= "air" then
		return
	end
	minetest.set_node(pos, {name="orewood:mesepapy"})
	end,
})

minetest.register_abm({
	nodenames = {"orewood:diamondwood", "orewood:diamondwood_jungle"},
	neighbors = {"default:water_source"},
	interval = 40,
	chance = 10,
	action = function(pos, node)
	local height = 0
	while node.name == "orewood:diamondwood" and height < 4 do
		height = height+1
		pos.y = pos.y+1
		node = minetest.get_node(pos)
	end
	while node.name == "orewood:diamondwood_jungle" and height < 4 do
		height = height+1
		pos.y = pos.y+1
		node = minetest.get_node(pos)
	end
	if height == 4
	or node.name ~= "air" then
		return
	end
	minetest.set_node(pos, {name="orewood:diamondpapy"})
	end,
})

minetest.register_abm({
	nodenames = {"orewood:goldwood", "orewood:goldwood_jungle"},
	neighbors = {"default:water_source"},
	interval = 40,
	chance = 10,
	action = function(pos, node)
	local height = 0
	while node.name == "orewood:goldwood" and height < 4 do
		height = height+1
		pos.y = pos.y+1
		node = minetest.get_node(pos)
	end
	while node.name == "orewood:goldwood_jungle" and height < 4 do
		height = height+1
		pos.y = pos.y+1
		node = minetest.get_node(pos)
	end
	if height == 4
	or node.name ~= "air" then
		return
	end
	minetest.set_node(pos, {name="orewood:goldpapy"})
	end,
})

minetest.register_abm({
	nodenames = {"orewood:goldwood_hot"},
	neighbors = {"default:lava_source"},
	interval = 40,
	chance = 10,
	action = function(pos, node)
	local height = 0
	while node.name == "orewood:goldwood_hot" and height < 4 do
		height = height+1
		pos.y = pos.y+1
		node = minetest.get_node(pos)
	end
	if height == 4
	or node.name ~= "air" then
		return
	end
	minetest.set_node(pos, {name="orewood:goldpapy_ingot"})
	end,
})

minetest.register_abm({
	nodenames = {"orewood:obsidianwood", "orewood:obsidianwood_jungle"},
	neighbors = {"default:water_source"},
	interval = 40,
	chance = 10,
	action = function(pos, node)
	local height = 0
	while node.name == "orewood:obsidianwood" and height < 4 do
		height = height+1
		pos.y = pos.y+1
		node = minetest.get_node(pos)
	end
	while node.name == "orewood:obsidianwood_jungle" and height < 4 do
		height = height+1
		pos.y = pos.y+1
		node = minetest.get_node(pos)
	end
	if height == 4
	or node.name ~= "air" then
		return
	end
	minetest.set_node(pos, {name="orewood:obsidianpapy"})
	end,
})

minetest.register_abm({
	nodenames = {"orewood:claywood", "orewood:claywood_jungle"},
	neighbors = {"default:water_source"},
	interval = 40,
	chance = 10,
	action = function(pos, node)
	local height = 0
	while node.name == "orewood:claywood" and height < 4 do
		height = height+1
		pos.y = pos.y+1
		node = minetest.get_node(pos)
	end
	while node.name == "orewood:claywood_jungle" and height < 4 do
		height = height+1
		pos.y = pos.y+1
		node = minetest.get_node(pos)
	end
	if height == 4
	or node.name ~= "air" then
		return
	end
	minetest.set_node(pos, {name="orewood:claypapy"})
	end,
})

minetest.register_abm({
	nodenames = {"orewood:claywood_hot"},
	neighbors = {"default:lava_source"},
	interval = 40,
	chance = 10,
	action = function(pos, node)
	local height = 0
	while node.name == "orewood:claywood_hot" and height < 4 do
		height = height+1
		pos.y = pos.y+1
		node = minetest.get_node(pos)
	end
	if height == 4
	or node.name ~= "air" then
		return
	end
	minetest.set_node(pos, {name="orewood:claypapy_brick"})
	end,
})

-- Functions to grow papy that has sprouted

minetest.register_abm({
	nodenames = {"orewood:ironpapy"},
	neighbors = {"orewood:ironwood", "orewood:ironwood_jungle"},
	interval = 50,
	chance = 20,
	action = function(pos, node)
	pos.y = pos.y-1
	local name = minetest.get_node(pos).name
	if name ~= "orewood:ironwood"
	and name ~= "orewood:ironwood_jungle" then
		return
	end
	if not minetest.find_node_near(pos, 3, {"group:water"}) then
		return
	end
	pos.y = pos.y+1
	local height = 0
	while node.name == "orewood:ironpapy" and height < 4 do
		height = height+1
		pos.y = pos.y+1
		node = minetest.get_node(pos)
	end
	if height == 4
	or node.name ~= "air" then
		return
	end
	minetest.set_node(pos, {name="orewood:ironpapy"})
	return true
end,
})

minetest.register_abm({
	nodenames = {"orewood:steelpapy_ingot"},
	neighbors = {"orewood:steelwood_hot"},
	interval = 50,
	chance = 20,
	action = function(pos, node)
	pos.y = pos.y-1
	local name = minetest.get_node(pos).name
	if name ~= "orewood:steelwood_hot"
	and name ~= "orewood:steelwood_hot" then
		return
	end
	if not minetest.find_node_near(pos, 3, {"group:lava"}) then
		return
	end
	pos.y = pos.y+1
	local height = 0
	while node.name == "orewood:steelpapy_ingot" and height < 4 do
		height = height+1
		pos.y = pos.y+1
		node = minetest.get_node(pos)
	end
	if height == 4
	or node.name ~= "air" then
		return
	end
	minetest.set_node(pos, {name="orewood:steelpapy_ingot"})
	return true
end,
})

minetest.register_abm({
	nodenames = {"orewood:coalpapy"},
	neighbors = {"orewood:coalwood", "orewood:coalwood_jungle"},
	interval = 50,
	chance = 20,
	action = function(pos, node)
	pos.y = pos.y-1
	local name = minetest.get_node(pos).name
	if name ~= "orewood:coalwood"
	and name ~= "orewood:coalwood_jungle" then
		return
	end
	if not minetest.find_node_near(pos, 3, {"group:water"}) then
		return
	end
	pos.y = pos.y+1
	local height = 0
	while node.name == "orewood:coalpapy" and height < 4 do
		height = height+1
		pos.y = pos.y+1
		node = minetest.get_node(pos)
	end
	if height == 4
	or node.name ~= "air" then
		return
	end
	minetest.set_node(pos, {name="orewood:coalpapy"})
	return true
end,
})

minetest.register_abm({
	nodenames = {"orewood:tinpapy"},
	neighbors = {"orewood:tinwood", "orewood:tinwood_jungle"},
	interval = 50,
	chance = 20,
	action = function(pos, node)
	pos.y = pos.y-1
	local name = minetest.get_node(pos).name
	if name ~= "orewood:tinwood"
	and name ~= "orewood:tinwood_jungle" then
		return
	end
	if not minetest.find_node_near(pos, 3, {"group:water"}) then
		return
	end
	pos.y = pos.y+1
	local height = 0
	while node.name == "orewood:tinpapy" and height < 4 do
		height = height+1
		pos.y = pos.y+1
		node = minetest.get_node(pos)
	end
	if height == 4
	or node.name ~= "air" then
		return
	end
	minetest.set_node(pos, {name="orewood:tinpapy"})
	return true
end,
})

minetest.register_abm({
	nodenames = {"orewood:tinpapy_ingot"},
	neighbors = {"orewood:tinwood_hot"},
	interval = 50,
	chance = 20,
	action = function(pos, node)
	pos.y = pos.y-1
	local name = minetest.get_node(pos).name
	if name ~= "orewood:tinwood_hot"
	and name ~= "orewood:tinwood_hot" then
		return
	end
	if not minetest.find_node_near(pos, 3, {"group:lava"}) then
		return
	end
	pos.y = pos.y+1
	local height = 0
	while node.name == "orewood:tinpapy_ingot" and height < 4 do
		height = height+1
		pos.y = pos.y+1
		node = minetest.get_node(pos)
	end
	if height == 4
	or node.name ~= "air" then
		return
	end
	minetest.set_node(pos, {name="orewood:tinpapy_ingot"})
	return true
end,
})

minetest.register_abm({
	nodenames = {"orewood:copperpapy"},
	neighbors = {"orewood:copperwood", "orewood:copperwood_jungle"},
	interval = 50,
	chance = 20,
	action = function(pos, node)
	pos.y = pos.y-1
	local name = minetest.get_node(pos).name
	if name ~= "orewood:copperwood"
	and name ~= "orewood:copperwood_jungle" then
		return
	end
	if not minetest.find_node_near(pos, 3, {"group:water"}) then
		return
	end
	pos.y = pos.y+1
	local height = 0
	while node.name == "orewood:copperpapy" and height < 4 do
		height = height+1
		pos.y = pos.y+1
		node = minetest.get_node(pos)
	end
	if height == 4
	or node.name ~= "air" then
		return
	end
	minetest.set_node(pos, {name="orewood:copperpapy"})
	return true
end,
})

minetest.register_abm({
	nodenames = {"orewood:copperpapy_ingot"},
	neighbors = {"orewood:copperwood_hot"},
	interval = 50,
	chance = 20,
	action = function(pos, node)
	pos.y = pos.y-1
	local name = minetest.get_node(pos).name
	if name ~= "orewood:copperwood_hot"
	and name ~= "orewood:copperwood_hot" then
		return
	end
	if not minetest.find_node_near(pos, 3, {"group:lava"}) then
		return
	end
	pos.y = pos.y+1
	local height = 0
	while node.name == "orewood:copperpapy_ingot" and height < 4 do
		height = height+1
		pos.y = pos.y+1
		node = minetest.get_node(pos)
	end
	if height == 4
	or node.name ~= "air" then
		return
	end
	minetest.set_node(pos, {name="orewood:copperpapy_ingot"})
	return true
end,
})

minetest.register_abm({
	nodenames = {"orewood:obsidianpapy"},
	neighbors = {"orewood:obsidianwood", "orewood:obsidianwood_jungle"},
	interval = 50,
	chance = 20,
	action = function(pos, node)
	pos.y = pos.y-1
	local name = minetest.get_node(pos).name
	if name ~= "orewood:obsidianwood"
	and name ~= "orewood:obsidianwood_jungle" then
		return
	end
	if not minetest.find_node_near(pos, 3, {"group:water"}) then
		return
	end
	pos.y = pos.y+1
	local height = 0
	while node.name == "orewood:obsidianpapy" and height < 4 do
		height = height+1
		pos.y = pos.y+1
		node = minetest.get_node(pos)
	end
	if height == 4
	or node.name ~= "air" then
		return
	end
	minetest.set_node(pos, {name="orewood:obsidianpapy"})
	return true
end,
})

minetest.register_abm({
	nodenames = {"orewood:diamondpapy"},
	neighbors = {"orewood:diamondwood", "orewood:diamondwood_jungle"},
	interval = 50,
	chance = 20,
	action = function(pos, node)
	pos.y = pos.y-1
	local name = minetest.get_node(pos).name
	if name ~= "orewood:diamondwood"
	and name ~= "orewood:diamondwood_jungle" then
		return
	end
	if not minetest.find_node_near(pos, 3, {"group:water"}) then
		return
	end
	pos.y = pos.y+1
	local height = 0
	while node.name == "orewood:diamondpapy" and height < 4 do
		height = height+1
		pos.y = pos.y+1
		node = minetest.get_node(pos)
	end
	if height == 4
	or node.name ~= "air" then
		return
	end
	minetest.set_node(pos, {name="orewood:diamondpapy"})
	return true
end,
})

minetest.register_abm({
	nodenames = {"orewood:mesepapy"},
	neighbors = {"orewood:mesewood", "orewood:mesewood_jungle"},
	interval = 50,
	chance = 20,
	action = function(pos, node)
	pos.y = pos.y-1
	local name = minetest.get_node(pos).name
	if name ~= "orewood:mesewood"
	and name ~= "orewood:mesewood_jungle" then
		return
	end
	if not minetest.find_node_near(pos, 3, {"group:water"}) then
		return
	end
	pos.y = pos.y+1
	local height = 0
	while node.name == "orewood:mesepapy" and height < 4 do
		height = height+1
		pos.y = pos.y+1
		node = minetest.get_node(pos)
	end
	if height == 4
	or node.name ~= "air" then
		return
	end
	minetest.set_node(pos, {name="orewood:mesepapy"})
	return true
end,
})

minetest.register_abm({
	nodenames = {"orewood:claypapy"},
	neighbors = {"orewood:claywood", "orewood:claywood_jungle"},
	interval = 50,
	chance = 20,
	action = function(pos, node)
	pos.y = pos.y-1
	local name = minetest.get_node(pos).name
	if name ~= "orewood:claywood"
	and name ~= "orewood:claywood_jungle" then
		return
	end
	if not minetest.find_node_near(pos, 3, {"group:water"}) then
		return
	end
	pos.y = pos.y+1
	local height = 0
	while node.name == "orewood:claypapy" and height < 4 do
		height = height+1
		pos.y = pos.y+1
		node = minetest.get_node(pos)
	end
	if height == 4
	or node.name ~= "air" then
		return
	end
	minetest.set_node(pos, {name="orewood:claypapy"})
	return true
end,
})

minetest.register_abm({
	nodenames = {"orewood:claypapy_brick"},
	neighbors = {"orewood:claywood_hot"},
	interval = 50,
	chance = 20,
	action = function(pos, node)
	pos.y = pos.y-1
	local name = minetest.get_node(pos).name
	if name ~= "orewood:claywood_hot"
	and name ~= "orewood:claywood_hot" then
		return
	end
	if not minetest.find_node_near(pos, 3, {"group:lava"}) then
		return
	end
	pos.y = pos.y+1
	local height = 0
	while node.name == "orewood:claypapy_brick" and height < 4 do
		height = height+1
		pos.y = pos.y+1
		node = minetest.get_node(pos)
	end
	if height == 4
	or node.name ~= "air" then
		return
	end
	minetest.set_node(pos, {name="orewood:claypapy_brick"})
	return true
end,
})

minetest.register_abm({
	nodenames = {"orewood:goldpapy"},
	neighbors = {"orewood:goldwood", "orewood:goldwood_jungle"},
	interval = 50,
	chance = 20,
	action = function(pos, node)
	pos.y = pos.y-1
	local name = minetest.get_node(pos).name
	if name ~= "orewood:goldwood"
	and name ~= "orewood:goldwood_jungle" then
		return
	end
	if not minetest.find_node_near(pos, 3, {"group:water"}) then
		return
	end
	pos.y = pos.y+1
	local height = 0
	while node.name == "orewood:goldpapy" and height < 4 do
		height = height+1
		pos.y = pos.y+1
		node = minetest.get_node(pos)
	end
	if height == 4
	or node.name ~= "air" then
		return
	end
	minetest.set_node(pos, {name="orewood:goldpapy"})
	return true
end,
})

minetest.register_abm({
	nodenames = {"orewood:goldpapy_ingot"},
	neighbors = {"orewood:goldwood_hot"},
	interval = 50,
	chance = 20,
	action = function(pos, node)
	pos.y = pos.y-1
	local name = minetest.get_node(pos).name
	if name ~= "orewood:goldwood_hot"
	and name ~= "orewood:goldwood_hot" then
		return
	end
	if not minetest.find_node_near(pos, 3, {"group:lava"}) then
		return
	end
	pos.y = pos.y+1
	local height = 0
	while node.name == "orewood:goldpapy_ingot" and height < 4 do
		height = height+1
		pos.y = pos.y+1
		node = minetest.get_node(pos)
	end
	if height == 4
	or node.name ~= "air" then
		return
	end
	minetest.set_node(pos, {name="orewood:goldpapy_ingot"})
	return true
end,
})
